package hlhj.fhp.tvlib.fragment

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.ImageTextBean
import hlhj.fhp.tvlib.utils.TimeUt
import kotlinx.android.synthetic.main.live_text_fgm.view.*
import java.util.*

/**
 * Created by Administrator on 2018\5\25 0025.
 */
class LiveTextFgm :Fragment(){
    private lateinit var rootView:View
    private var id=""
    private lateinit var adp:BaseQuickAdapter<ImageTextBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<ImageTextBean.DataBean>()
    private lateinit var loadingDialog:LoadingDialog
    private lateinit var timer:Timer
    private var sTime=""
    private val hanlder=object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            despatchData()
            this.sendEmptyMessageDelayed(1,20000)
        }
    }
    companion object {
        fun getInstentce(id:String): LiveTextFgm {
            val fgm = LiveTextFgm()
            val bundle = Bundle()
            bundle.putString("id",id)
            fgm.arguments=bundle
            return fgm
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        hanlder.removeMessages(1)
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.live_text_fgm,null)
        id=arguments!!.getString("id")
        loadingDialog= LoadingDialog(context!!)

        initRcy()
        timer = Timer()

        val task=object :TimerTask(){
            override fun run() {
                hanlder.sendEmptyMessage(1)
            }
        }

        timer.schedule(task,2000)
        return rootView
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (timer!=null){
            timer.cancel()
        }
    }
    private fun initRcy() {
        adp=object :BaseQuickAdapter<ImageTextBean.DataBean,BaseViewHolder>(R.layout.live_text_item,datas){
            override fun convert(helper: BaseViewHolder?, item: ImageTextBean.DataBean?) {
                helper?.setText(R.id.itemTittle,TimeUt.timeStampToStr(item?.update_at!!.toLong()))
                helper?.setText(R.id.itemContent,item?.online_content)
                val imageView = helper?.getView<ImageView>(R.id.itemImg)
                Glide.with(mContext).load(Contents.BaseUrl+item?.online_thumb).into(imageView!!)

            }
        }
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        rootView.recyclerview.adapter=adp
    }

    private fun despatchData() {
        Log.e("fhp","stime=${sTime}---etime=${TimeUt.timeStampToStr(System.currentTimeMillis())}")
        OkGo.get<String>(Contents.BaseUrl+Contents.TEXT_LIVE)
                .params("live_id",id)
                .params("start_time",sTime)
                .params("last_time",TimeUt.timeStampToStr(System.currentTimeMillis()))
                .execute(object :StringCallback(){
                    override fun onSuccess(response: Response<String>?) {
                        val str = response?.body()
                        val imageTextBean = Gson().fromJson(str, ImageTextBean::class.java)
                        if (imageTextBean.code==1){
                            sTime=imageTextBean.lt
                            datas.addAll(imageTextBean.data)
                            if (imageTextBean.data.size>0){
                                adp.notifyItemInserted(0)
                            }else{
                                adp.notifyDataSetChanged()
                            }
                            rootView.recyclerview.scrollToPosition(0)
                        }
                    }
                })
    }
}